package actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

public class CopyAction implements Action {
	
	protected String _copyTo;

	//Assumes input of src WITH '\' and copyTo WITHOUT '\' - consider it
	//in building the actions
	public CopyAction (String src, String copyTo){
		_copyTo = src.concat(copyTo);
		File f = new File(_copyTo);
		if (!f.exists())
			f.mkdirs();
	}
	
	
	
	public void doAction(File file, ArrayList<File> files){
		File target = new File (_copyTo + file.getName());
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new FileInputStream(file);
			out = new FileOutputStream(target);
			int res;
			while ((res = in.read()) != -1) {
				out.write(res);
			}
			
		}
		catch (IOException e){
			throw new IOException();
		}
		finally {
			try {
				if (in != null)
					in.close();
			}
			catch (IOException e){
				throw new IOException();
			}
			try {
				if (out != null)
					out.close();
			}
			catch (IOException e){
				throw new IOException();
			}
		}
		files.add(target);
	}
	//Tester
	public static void main (String args[]){
		File file = new File ("C:\\Intel\\cmd.txt");
		String src = "C:\\Intel\\";
		String tar = "ccc\\";
		file.setReadable(false);
		CopyAction h = new CopyAction(src, tar);
		h.doAction(file);
	}
	
	

}
